package com.ruoyi.report_jl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_jl.domain.bo.ReportJlCjlsfgsfjlbBaDetailBo;
import com.ruoyi.report_jl.domain.vo.ReportJlCjlsfgsfjlbBaDetailVo;
import com.ruoyi.report_jl.domain.ReportJlCjlsfgsfjlbBaDetail;
import com.ruoyi.report_jl.mapper.ReportJlCjlsfgsfjlbBaDetailMapper;
import com.ruoyi.report_jl.service.IReportJlCjlsfgsfjlbBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 粗集料筛分试验检测记录（干筛法）详情Service业务层处理
 *
 * @author tll
 * @date 2024-05-22
 */
@RequiredArgsConstructor
@Service
public class ReportJlCjlsfgsfjlbBaDetailServiceImpl implements IReportJlCjlsfgsfjlbBaDetailService {

    private final ReportJlCjlsfgsfjlbBaDetailMapper baseMapper;

    /**
     * 查询粗集料筛分试验检测记录（干筛法）详情
     */
    @Override
    public ReportJlCjlsfgsfjlbBaDetailVo queryById(Long detailId){
        return baseMapper.selectVoById(detailId);
    }

    /**
     * 查询粗集料筛分试验检测记录（干筛法）详情列表
     */
    @Override
    public TableDataInfo<ReportJlCjlsfgsfjlbBaDetailVo> queryPageList(ReportJlCjlsfgsfjlbBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportJlCjlsfgsfjlbBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportJlCjlsfgsfjlbBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询粗集料筛分试验检测记录（干筛法）详情列表
     */
    @Override
    public List<ReportJlCjlsfgsfjlbBaDetailVo> queryList(ReportJlCjlsfgsfjlbBaDetailBo bo) {
        LambdaQueryWrapper<ReportJlCjlsfgsfjlbBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportJlCjlsfgsfjlbBaDetail> buildQueryWrapper(ReportJlCjlsfgsfjlbBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportJlCjlsfgsfjlbBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportJlCjlsfgsfjlbBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getShaifhzzlItem1() != null, ReportJlCjlsfgsfjlbBaDetail::getShaifhzzlItem1, bo.getShaifhzzlItem1());
        lqw.eq(bo.getSunhItem1() != null, ReportJlCjlsfgsfjlbBaDetail::getSunhItem1, bo.getSunhItem1());
        lqw.eq(bo.getSunhlItem1() != null, ReportJlCjlsfgsfjlbBaDetail::getSunhlItem1, bo.getSunhlItem1());
        lqw.eq(bo.getShaifhzzlItem2() != null, ReportJlCjlsfgsfjlbBaDetail::getShaifhzzlItem2, bo.getShaifhzzlItem2());
        lqw.eq(bo.getSunhItem2() != null, ReportJlCjlsfgsfjlbBaDetail::getSunhItem2, bo.getSunhItem2());
        lqw.eq(bo.getSunhlItem2() != null, ReportJlCjlsfgsfjlbBaDetail::getSunhlItem2, bo.getSunhlItem2());
        lqw.eq(StringUtils.isNotBlank(bo.getShaikcc()), ReportJlCjlsfgsfjlbBaDetail::getShaikcc, bo.getShaikcc());
        lqw.eq(StringUtils.isNotBlank(bo.getChenglcsItem1()), ReportJlCjlsfgsfjlbBaDetail::getChenglcsItem1, bo.getChenglcsItem1());
        lqw.eq(bo.getZhilItem1() != null, ReportJlCjlsfgsfjlbBaDetail::getZhilItem1, bo.getZhilItem1());
        lqw.eq(bo.getShaiszlItem1() != null, ReportJlCjlsfgsfjlbBaDetail::getShaiszlItem1, bo.getShaiszlItem1());
        lqw.eq(bo.getFenjsyItem1() != null, ReportJlCjlsfgsfjlbBaDetail::getFenjsyItem1, bo.getFenjsyItem1());
        lqw.eq(bo.getLeijsyItem1() != null, ReportJlCjlsfgsfjlbBaDetail::getLeijsyItem1, bo.getLeijsyItem1());
        lqw.eq(bo.getTonggbflItem1() != null, ReportJlCjlsfgsfjlbBaDetail::getTonggbflItem1, bo.getTonggbflItem1());
        lqw.eq(StringUtils.isNotBlank(bo.getChenglcsItem2()), ReportJlCjlsfgsfjlbBaDetail::getChenglcsItem2, bo.getChenglcsItem2());
        lqw.eq(bo.getZhilItem2() != null, ReportJlCjlsfgsfjlbBaDetail::getZhilItem2, bo.getZhilItem2());
        lqw.eq(bo.getShaiszlItem2() != null, ReportJlCjlsfgsfjlbBaDetail::getShaiszlItem2, bo.getShaiszlItem2());
        lqw.eq(bo.getFenjsyItem2() != null, ReportJlCjlsfgsfjlbBaDetail::getFenjsyItem2, bo.getFenjsyItem2());
        lqw.eq(bo.getLeijsyItem2() != null, ReportJlCjlsfgsfjlbBaDetail::getLeijsyItem2, bo.getLeijsyItem2());
        lqw.eq(bo.getTonggbflItem2() != null, ReportJlCjlsfgsfjlbBaDetail::getTonggbflItem2, bo.getTonggbflItem2());
        lqw.eq(bo.getLeijsybflPzh() != null, ReportJlCjlsfgsfjlbBaDetail::getLeijsybflPzh, bo.getLeijsybflPzh());
        return lqw;
    }

    /**
     * 新增粗集料筛分试验检测记录（干筛法）详情
     */
    @Override
    public Boolean insertByBo(ReportJlCjlsfgsfjlbBaDetailBo bo) {
        ReportJlCjlsfgsfjlbBaDetail add = BeanUtil.toBean(bo, ReportJlCjlsfgsfjlbBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDetailId(add.getDetailId());
        }
        return flag;
    }

    /**
     * 修改粗集料筛分试验检测记录（干筛法）详情
     */
    @Override
    public Boolean updateByBo(ReportJlCjlsfgsfjlbBaDetailBo bo) {
        ReportJlCjlsfgsfjlbBaDetail update = BeanUtil.toBean(bo, ReportJlCjlsfgsfjlbBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportJlCjlsfgsfjlbBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除粗集料筛分试验检测记录（干筛法）详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
